<?php

namespace App\Repositories;
use App\Models\Tag;

class TagRepository extends BaseRepository
{

    public function __construct(Tag $tag)
    {
        $this->model = $tag;
    }

    public function all()
    {
        $tags = $this->model->all();
        return $tags;
    }


    /**
     * 创建或更新Role
     *
     * @param  App\Role $tag
     * @param  array $inputs
     * @return App\Role
     */
    private function saveTag($tag, $inputs)
    {
        $tag->name = e($inputs['name']);
        $tag->title = e($inputs['title']);
        $tag->sort = e($inputs['sort'])?e($inputs['sort']):0;
        $tag->description = e($inputs['description']) ;
        if($inputs['pid']){
            $p_model = $this->getById($inputs['pid']);
            $tag->pid = $p_model?$p_model->id:0;
            $tag->level = $p_model?$p_model->level+1:1;
        }
        if ($tag->save()) {
//            if (array_key_exists('permissions', $inputs)) {
//                $permissions = $inputs['permissions'];  //这里提交的为数组
//                if (is_array($permissions) && $permissions) {
//                    $tag->perms()->sync($permissions);  //同步角色权限
//                }
//            } else {
//                $tag->perms()->sync([]);
//            }
        }

        return $tag;
    }

    public function index($data = [], $extra = '', $size = 10)
    {
        $flow = $this->model;
        if($extra)
            $flow = $flow->where($extra);
        return $flow->orderBy('sort','desc')->paginate($size);
    }

    public function store($inputs, $extra = '')
    {
        if(e($inputs['id'])){
            $tag = $this->edit(e($inputs['id']));
        }else {
            $tag = new $this->model;
        }
        $tag = $this->saveTag($tag, $inputs);
        return $tag;
    }

    /**
     * 获取编辑的标签
     *
     * @param  int $id
     * @param  string|array $extra
     * @return App\Tag
     */
    public function edit($id, $extra = '')
    {
        $tag = $this->getById($id);
        return $tag;
    }

    /**
     * 更新角色
     *
     * @param  int $id
     * @param  array $inputs
     * @param  string|array $extra
     * @return void
     */
    public function update($id, $inputs, $extra = '')
    {
        $tag = $this->getById($id);
        return $this->saveTag($tag, $inputs);
    }

    #********
    #* 资源 REST 相关的接口函数 END
    #********

    public function getTree($id)
    {
        $data = $this->model->where('pid',$id)->orderBy('sort','desc')->get();
        if($data) {
            foreach ($data as &$v) {
                $v['subclass'] = $this->getTree($v->id);
            }
        }
        return $data;
    }

    public function treeSort($_data)
    {
        $data = [];
        foreach ($_data as $v) {
            $data[] = $v;
            if($v['subclass']) {
                $this->_rest($data, $v['subclass']);
                unset($v['subclass']);
            }
        }
        return $data;
    }

    public function _rest(&$data,&$arr)
    {
        foreach ($arr as &$v) {
            $data[] = $v;
            if($v['subclass']) {
                $this->_rest($data, $v['subclass']);
                unset($v['subclass']);
            }
        }
    }
}
